package net.mingsoft.blackbox;

import lombok.extern.slf4j.Slf4j;
import net.mingsoft.basic.entity.LogEntity;
import net.mingsoft.examine.utils.DateUtil;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Map;
import java.util.Set;

/**
 * 黑盒日志发送类
 * BlackboxUtil
 * @author xzc
 * @date 2019-09-05 17:18
 */
@Slf4j
public class BlackboxUtil {

    //webservice地址
    private static String url="http://10.1.3.15/services/LogSubmitWebService?wsdl";
    private static String systemId = "population";
    private static String version = "2.0";

    /**
     * 黑盒日志发送
     * @return
     */
    public static boolean sendLog(LogEntity sysAuditLog){
        try {
            //定义一个map来装载所有日志参数
            Map<String, Object> map = new HashedMap();
            //必传字段
            map.put("systemId",systemId);
            map.put("version",version);
            map.put("operateType",getType(sysAuditLog.getLogBusinessType()));
            //自定义字段
            map.put("deptId","null");//部门有多个,所以暂时置空为空
            map.put("userId",String.valueOf(sysAuditLog.getLogUser()));
            map.put("userName",sysAuditLog.getLogUser());
            map.put("ip",sysAuditLog.getLogIp());
            map.put("moduleName",sysAuditLog.getLogUrl());
            map.put("behavior",sysAuditLog.getLogTitle());
            map.put("content",sysAuditLog.getLogResult());
            map.put("createTime", DateUtil.dateToString(sysAuditLog.getCreateDate(),"yyyy-MM-dd HH:mm:ss"));
            //map转化成指定xml格式的字符串
            Set<String> keys = map.keySet();
            Document document = DocumentHelper.createDocument();
            document.setXMLEncoding("UTF-8");
            Element root =document.addElement("OperateLog");
            for (String key : keys) {
                root.addElement("Item")
                        .addAttribute("key", key)
                        .addAttribute("value", (String) map.get(key));
            }
            String text = root.asXML();
            log.info(text);
            //webservice地址调用
            JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
            factoryBean.setAddress(url);
            factoryBean.setServiceClass(BlackBoxService.class);
            BlackBoxService submitService = (BlackBoxService) factoryBean.create();
            String object = submitService.logSubmit(text);
            log.info("黑盒日志发送成功{}",object);
            return true;
        }catch (Exception e){
            log.info("黑盒日志发送失败{}",e.getMessage());
            return false;
        }

    }

    private static String getType(String code){
        String operateType=null;

        if (code.equals("other")){
            operateType="query";
        }else if (code.equals("insert")){
            operateType="create";
        }else if (code.equals("delete")){
            operateType="delete";
        }else if (code.equals("update")){
            operateType="update";
        }
        return operateType;
    }
}
